﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Data.EntityClient;
using WEB.DAL.Models;
using DAL.BAL;

namespace WpfClient.Windows
{
    /// <summary>
    /// Interaction logic for RefreshSiteData.xaml
    /// </summary>
    public partial class RefreshSiteData : Window
    {
        public RefreshSiteData()
        {
            InitializeComponent();
        }

        private void bSync_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder();

                ecsb.Name = "WebSiteEnteties";

                EntityConnection ec = new EntityConnection(ecsb.ConnectionString);

                string res = string.Empty;

                res = DeleteItems(ec);

                if (res != string.Empty)
                    MessageBox.Show(res, WpfClient.Properties.Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
                else
                {
                    res = InsertItems(ec);

                    if (res != string.Empty)
                        MessageBox.Show(res, WpfClient.Properties.Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
                    else
                    {
                        MessageBox.Show("Данные успешно синхронизированы", WpfClient.Properties.Resources.Warning, MessageBoxButton.OK, MessageBoxImage.Information);

                        Close();
                    }
                }
            }
            catch (Exception ex)
            {
                progressBar1.Value = 0;

                progressBar2.Value = 0;

                MessageBox.Show(ex.Message, WpfClient.Properties.Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private string DeleteItems(EntityConnection connection)
        {
            using (WebSiteEnteties websiteEntities = new WebSiteEnteties(connection))
            {
                var candidates = from n in websiteEntities.Candidates
                                 select n;

                progressBar1.Minimum = 0;

                progressBar1.Maximum = candidates.Count();

                int i = 0;

                foreach (Candidate candidate in candidates)
                {
                    websiteEntities.Candidates.DeleteObject(candidate);

                    i++;

                    progressBar1.Value = i;

                    Classes.DispatcherHelper.DoEvents();
                }

                foreach (Position pos in websiteEntities.Positions)
                {
                    websiteEntities.Positions.DeleteObject(pos);
                }

                websiteEntities.SaveChanges();

                return string.Empty;
            }
        }

        private string InsertItems(EntityConnection connection)
        {
            #region Sync positions
            using (WebSiteEnteties websiteEntities = new WebSiteEnteties(connection))
            {
                foreach (Position p in from n in Business.dosvidEntities.Positions
                                       select new Position { Id = n.Id, PositionName = n.PositionName })
                {
                    websiteEntities.Positions.AddObject(p);

                }
                websiteEntities.SaveChanges();
            }
            #endregion

            var candidates = from n in Business.dosvidEntities.Candidates
                             where n.Candidate_Types.Id == 4 && n.IsCandidate == true &&
                                   n.ApproveOnPost == true
                             select n;
            
            progressBar2.Minimum = 0;

            progressBar2.Maximum = candidates.Count();

            int i = 0;

            foreach (DAL.Models.Candidate candidate in candidates)
            {
                string res = Classes.SyncCandidateWithWebSite.AddToWebSite(candidate);

                if (res != string.Empty)
                {
                    return res;                    
                }
                
                i++;

                progressBar2.Value = i;

                Classes.DispatcherHelper.DoEvents();

            }

            return string.Empty;
        }
    }
}

